﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	LinkValue.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Diagnostics;

namespace Smart.Common.Entities
{
	/// <summary>
	/// Link value
	/// </summary>
	[DebuggerStepThrough]
	public class LinkValue
	{
		/// <summary>
		/// Link Url
		/// </summary>
		public string Url { get; set; }

		/// <summary>
		/// Link Title
		/// </summary>
		public string Title { get; set; }

		/// <summary>
		/// Link value
		/// </summary>
		public LinkValue(string url, string title)
		{
			Url = url;
			Title = title;
		}

		/// <summary>
		/// Checks whether this value is empty
		/// </summary>
		public bool IsEmpty
		{
			get { return string.IsNullOrEmpty(Url); }
		}

		/// <summary>
		/// Set new lookup value
		/// </summary>
		public void Set(string url)
		{
			Url = url;
			Title = null;
		}

		/// <summary>
		/// Set new lookup value
		/// </summary>
		public void Set(string url, string title)
		{
			Url = url;
			Title = title;
		}

		/// <summary>
		/// Format in "url, title"
		/// </summary>
		public override string ToString()
		{
			return (IsEmpty ? string.Empty : Url + ", " + Title);
		}

		/// <summary>
		/// Compare values using OrdinalIgnoreCase
		/// </summary>
		public bool Equals(LinkValue lv)
		{
			return
				Url.EqIC(lv.Url) &&
				Title.EqIC(lv.Title);
		}

		/// <summary>
		/// Compare values using OrdinalIgnoreCase
		/// </summary>
		public override bool Equals(object obj)
		{
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			return
				Equals((LinkValue)obj);
		}

		/// <summary>
		/// Hash code using OrdinalIgnoreCase
		/// </summary>
		public override int GetHashCode()
		{
			return
				StringComparer.OrdinalIgnoreCase.GetHashCode(Url) ^
				StringComparer.OrdinalIgnoreCase.GetHashCode(Title);
		}

		public static bool operator ==(LinkValue lv1, LinkValue lv2)
		{
			return Equals(lv1, lv2);
		}

		public static bool operator !=(LinkValue lv1, LinkValue lv2)
		{
			return !Equals(lv1, lv2);
		}

		public static bool operator ==(LinkValue lv1, string lv2)
		{
			if (ReferenceEquals(lv1, null) && lv2 == null)
				return true;

			if (ReferenceEquals(lv1, null) || lv2 == null)
				return false;

			return
				lv1.Url.EqIC(lv2);
		}

		public static bool operator !=(LinkValue lv1, string lv2)
		{
			if (ReferenceEquals(lv1, null) && lv2 == null)
				return false;

			if (ReferenceEquals(lv1, null) || lv2 == null)
				return true;

			return
				!lv1.Url.EqIC(lv2);
		}
	}
}